﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace octopus_project.enumeration {

    public enum TileType : byte {

        PLAYER_CONQUERED = 0,
        PLAYER_WALL = 1,
        PLAYER_CURSOR = 2,
        PLAYER_CASTLE =3,
        PLAYER_CASTLE_R_UP = 4,
        PLAYER_CASTLE_L_DWN = 5,
        PLAYER_CASTLE_R_DWN = 6,
        PLAYER_DOUBLE_WALL = 7,
        

        ///EMPIEZAN EN EL 40 LOS GENERICOS
        NOT_CONQUERED = 40,
        UNAVAILABLE = 41,
        PLAYER_MINI_CANNON = 42,
        NOT_CONQUERED_B = 43,
        MEDIUM_CANNON = 44,
        MEDIUM_CANNON_R_UP = 45,
        MEDIUM_CANNON_L_DWN = 46,
        MEDIUM_CANNON_R_DWN = 47,
        DESTROY = 48


    }

    public static class TileTypeExtensions {

        static public Texture2D SpriteImage;

        public static TileType getFromValue(byte tileTypeValue) {
            string name = Enum.GetName(typeof(TileType), tileTypeValue);
            return (TileType)Enum.Parse(typeof(TileType), name);
        }


        /*static public Rectangle GetSourceRectangle (TileType tileType, TypesTileType typesTileType) {
            return new Rectangle((byte)tileType * 32, (byte)typesTileType * 32, 32, 32);
        }*/

        static public Rectangle GetSourceRectangle(this TileType tileType, PlayersColor playersColor = PlayersColor.UNDEFINED) {
            int tileY = (byte)tileType / (SpriteImage.Width / Constant.TILE_IMAG_SIDE);
            int tileX = (byte)tileType % (SpriteImage.Width / Constant.TILE_IMAG_SIDE);

            if ((byte)tileType < (byte)TileType.NOT_CONQUERED && playersColor != PlayersColor.UNDEFINED) {

                int realType = (byte)tileType + (byte)playersColor * (byte)(SpriteImage.Width / Constant.TILE_IMAG_SIDE);
                tileY = (byte)realType / (SpriteImage.Width / Constant.TILE_IMAG_SIDE);
                tileX = (byte)realType % (SpriteImage.Width / Constant.TILE_IMAG_SIDE);

            }
            else {

                tileY = (byte)tileType / (SpriteImage.Width / Constant.TILE_IMAG_SIDE);
                tileX = (byte)tileType % (SpriteImage.Width / Constant.TILE_IMAG_SIDE);
            }
            return new Rectangle(tileX * Constant.TILE_IMAG_SIDE, tileY * Constant.TILE_IMAG_SIDE, Constant.TILE_IMAG_SIDE, Constant.TILE_IMAG_SIDE);
        }
    }
}
